package cn.ac.iscas.dao.provider.dameng;

import cn.ac.iscas.domain.job.JobSearch;
import org.apache.ibatis.jdbc.SQL;

/**
 * @author LJian
 * @version 1.0
 * @description: TODO
 * @date 2023/1/13 18:17
 */
public class DamengComponentMetadataProvider {
    public String select(final String type, final String modelType){
        //todo:mysql，如果使用mysql，打开以下注释，同时注释掉dameng
        /*
        return new SQL(){{
            SELECT( " _id, entryClassName, kind, alias, description, icon, category, categoryAlias, modelType, type, algorithm, type, viewParameter, " +
                    " modelLocalPath, modelPublishPath, columnConfig " );
            FROM("componentmetadata");
            if(type != null && !type.trim().isEmpty()){
                WHERE("type = #{type}");
            }
            if(modelType != null && !modelType.trim().isEmpty()){
                WHERE("modelType = #{modelType}");
            }
        }}.toString();
        */

        //todo:dameng,需要加上convert(varchar, viewParameter)，viewParameter在达梦中是text类型，不加convert会解析成DmdbNClob类型，导致json无限嵌套报错
        return new SQL(){{
            SELECT( " _id, entryClassName, kind, alias, description, icon, category, categoryAlias, modelType, type, algorithm, type, viewParameter, " +
                    " modelLocalPath, modelPublishPath, columnConfig " );
            FROM("componentmetadata");
            if(type != null && !type.trim().isEmpty()){
                WHERE("type = #{type}");
            }
            if(modelType != null && !modelType.trim().isEmpty()){
                WHERE("modelType = #{modelType}");
            }
        }}.toString();
    }

    public String selectById(final String id){
        //todo:mysql，如果使用mysql，打开以下注释，同时注释掉dameng
        /*
        return new SQL(){{
            SELECT(" _id, entryClassName, kind, alias, description, icon, modelType, algorithm, type, viewParameter, " +
                    " modelLocalPath, modelPublishPath, columnConfig ");
            FROM("componentmetadata");
            if(id != null && !id.trim().isEmpty()){
                WHERE("_id = #{id}");
            }
        }}.toString();
        */

        //todo:dameng,需要加上convert(varchar, viewParameter)，viewParameter在达梦中是text类型，不加convert会解析成DmdbNClob类型，导致json无限嵌套报错
        return new SQL(){{
            SELECT(" _id, entryClassName, kind, alias, description, icon, modelType, algorithm, type, viewParameter, " +
                    " modelLocalPath, modelPublishPath, columnConfig ");
            FROM("componentmetadata");
            if(id != null && !id.trim().isEmpty()){
                WHERE("_id = #{id}");
            }
        }}.toString();
    }

    public String selectByCategory(final String category){
        if (category == null || category.isEmpty()) {
            return "";
        }
        return new SQL(){{
            SELECT(" _id, entryClassName, kind, alias, description, icon, modelType, algorithm, type, viewParameter, " +
                    " modelLocalPath, modelPublishPath, columnConfig ");
            FROM("componentmetadata");
            if(category != null && !category.trim().isEmpty()) {
                WHERE("category LIKE CONCAT('%',#{category},'%')");
            }
        }}.toString();
    }
}
